package com.example.dirviesystem.coach.controller;

import com.example.dirviesystem.config.R;
import com.example.dirviesystem.entity.CoachInfo;
import com.example.dirviesystem.entity.DrivingType;
import com.example.dirviesystem.coach.service.CoachInfoService;
import com.example.dirviesystem.coach.service.DrivingTypeService;
import com.example.dirviesystem.repository.CoachInfoRe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/coaches")
@CrossOrigin(origins = "http://localhost:5173")
public class CoachInfoController {

    @Autowired
    private CoachInfoService coachInfoService;
    @Autowired
    private DrivingTypeService drivingTypeService;
    @Autowired
    private CoachInfoRe  coachInfoRe;

    @PostMapping("/add")
    public ResponseEntity<CoachInfo> addCoach(@RequestBody CoachInfo coachInfo) {
        // 查找 DrivingType 实体
        DrivingType drivingType = drivingTypeService.findByCode(coachInfo.getDrivingType().getCode());
        if (drivingType != null) {
            coachInfo.setDrivingType(drivingType);
        } else {
            return ResponseEntity.badRequest().body(null);
        }

        CoachInfo savedCoach = coachInfoService.save(coachInfo);
        return ResponseEntity.ok(savedCoach);
    }

    @GetMapping("/{id}")
    public ResponseEntity<CoachInfo> getCoachById(@PathVariable Long id) {
        CoachInfo coachInfo = coachInfoService.findById(id);
        if (coachInfo != null) {
            return ResponseEntity.ok(coachInfo);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<CoachInfo> updateCoach(@PathVariable Long id, @RequestBody CoachInfo coachInfo) {
        CoachInfo existingCoach = coachInfoService.findById(id);
        if (existingCoach != null) {
            existingCoach.setName(coachInfo.getName());
            existingCoach.setGender(coachInfo.getGender());
            existingCoach.setPhone(coachInfo.getPhone());
            existingCoach.setIdNumber(coachInfo.getIdNumber());

            // 更新驾考类型
            DrivingType drivingType = drivingTypeService.findByCode(coachInfo.getDrivingType().getCode());
            if (drivingType != null) {
                existingCoach.setDrivingType(drivingType);
            }

            existingCoach.setYearsOfExperience(coachInfo.getYearsOfExperience());
            existingCoach.setPassword(coachInfo.getPassword()); // 注意安全性
            coachInfoService.save(existingCoach);
            return ResponseEntity.ok(existingCoach);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    @GetMapping("/getCoaches/{typeId}")
    public R<List<CoachInfo>> getCoachesByDrivingType(@PathVariable String typeId) throws Exception {
        System.out.println("学生选择的报名类型"+typeId);
        List<CoachInfo> coachInfos = coachInfoRe.findAllByDrivingTypeCode(typeId);
        List<CoachInfo> coachInfos1 =coachInfos.stream()
                .filter(coachInfo -> {
                    return coachInfo.getIsQualified();
                })
                .toList();
        System.out.println(coachInfos1);
        return R.success(coachInfos1);
    }

    @PutMapping("/change-password/{id}")
    public ResponseEntity<String> changePassword(@PathVariable Long id, @RequestBody Map<String, String> passwords) {
        CoachInfo existingCoach = coachInfoService.findById(id);
        if (existingCoach != null) {
            // 验证原密码
            if (!existingCoach.getPassword().equals(passwords.get("oldPassword"))) {
                return ResponseEntity.badRequest().body("原密码不正确");
            }
            // 更新新密码
            existingCoach.setPassword(passwords.get("newPassword"));
            coachInfoService.save(existingCoach);
            return ResponseEntity.ok("密码修改成功");
        } else {
            return ResponseEntity.notFound().build();
        }
    }


}